* GRAVITY.do
* DO file for estimating commuting gravity equation 
* Using commuting costs from logit estimation (Step 1)

graph set window fontface "Ariel"
ssc install ftools, replace
ssc install require, replace
ssc install ppmlhdfe, replace
ssc install reghdfe, replace
ssc install estout, replace
********************************************************
***** Person Trip Survey Data **************************
***** and Estimated Commuting Costs (Step 1) ***********
********************************************************
use "../../data/raw data/commuting/bilateralflow_traveltime.dta", clear 

/* Merge with Estimated Travel Cost */
merge 1:1 origin_id destination_id using "../../data/raw data/commuting/modechoice.dta" 

/* Define left hand side */ 
gen l_volume = log(volume)
gen l_volume_adh = log(volume + 1)   /* ad-hoc log (+1) */

/* Straight line distance */ 
gen l_distance = log(distance)

********************************************************************
***** Gravity Equation Estimation **********************************
***** Using the Estimated Cost of Commuting (in Nested Logit) ******
***** OLS, OLS with full bilateral commutings, PPML ****************
***** All specifications include inner commuting *******************
***** Samples more than 20 commuters *******************************
********************************************************************
/* OLS regression */
reghdfe l_volume mode_cost_1987, absorb(i.origin_id  i.destination_id) vce(robust) 
est sto gravity1 

reghdfe l_volume mode_cost_1987 if vol > 20, absorb(i.origin_id  i.destination_id) vce(robust) 
est sto gravity2

/* OLS regression. +1 log */
reghdfe l_volume_adh mode_cost_1987, absorb(i.origin_id  i.destination_id) vce(robust) 
est sto gravity3

reghdfe l_volume_adh mode_cost_1987 if vol > 20, absorb(i.origin_id  i.destination_id) vce(robust) 
est sto gravity4

/* PPML regression */
ppmlhdfe volume mode_cost_1987, absorb(i.origin_id  i.destination_id) vce(robust)
est sto gravity5 

ppmlhdfe volume mode_cost_1987 if vol > 20, absorb(i.origin_id  i.destination_id) vce(robust)
est sto gravity6

esttab gravity1 gravity2 gravity3 gravity4 gravity5 gravity6  using "../../output/table/tableF1_gravity_regression_logitcost.tex", se scalars(r2_a r2_p) star(c 0.1 b 0.05 a 0.01) b(3) keep(mode_cost_1987) replace

/* Instrumenting OLS regressions by straight distance */
* ivreghdfe: https://github.com/sergiocorreia/ivreghdfe#installation

* Strong correlation between travel cost and stright distance
reg mode_cost_1987 distance
corr mode_cost_1987 distance

ivregress 2sls l_volume (mode_cost_1987 = distance) i.origin_id  i.destination_id, robust first
est sto gravity1_iv

ivregress 2sls l_volume (mode_cost_1987 = distance) i.origin_id  i.destination_id if vol>20, robust first
est sto gravity2_iv

/* OLS regression. +1 log */
ivregress 2sls l_volume_adh (mode_cost_1987 = distance) i.origin_id  i.destination_id, robust first
est sto gravity3_iv

ivregress 2sls l_volume_adh (mode_cost_1987 = distance) i.origin_id  i.destination_id if vol>20, robust first
est sto gravity4_iv

esttab gravity1_iv gravity2_iv gravity3_iv gravity4_iv using "../../output/table/tableF1_gravity_regression_logitcost_iv.tex", se scalars(r2_a r2_p) star(c 0.1 b 0.05 a 0.01) b(3) keep(mode_cost_1987) replace


